package cn.learn.controller.module;

import cn.learn.base.BaseController;
import cn.learn.domain.module.Module;
import cn.learn.service.module.ModuleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 展示权限列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "展示模块")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo resultData = moduleService.selectByPage(pageNum, pageSize);
        request.setAttribute("pageList", resultData);
        return "/system/module/module-list";
    }

    /**
     * 添加新模块 与 跳转页面
     * @param module
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加模块")
    public String addOne(Module module) {
        if (request.getMethod().equals("POST")) {
            String uid = UUID.randomUUID().toString().replace("-", "");
            module.setId(uid);
            moduleService.addOne(module);
            return "redirect:/system/module/list.do";
        }
        List<Module> resultData = moduleService.selectAll();
        request.setAttribute("menus", resultData);
        return "/system/module/module-add";
    }

    /**
     * 修改模块 与 跳转修改页面
     * @param module
     * @return menus
     */
    @RequestMapping(value = "/updateOne", name = "修改模块")
    public String updateOne(Module module) {
        if (request.getMethod().equals("POST")) {
            moduleService.updateOneById(module);
            return "redirect:/system/module/list.do";
        }
        Module resultModule = moduleService.findOneById(module);
        List<Module> resultData = moduleService.selectAll();
        request.setAttribute("data", resultModule);
        request.setAttribute("menus", resultData);
        return "/system/module/module-update";
    }

    /**
     * 根据id删除一条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "删除模块")
    public String delete(String id) {
        moduleService.deleteOneById(id);
        return "redirect:/system/module/list.do";
    }

}
